public class HappyTree {
    class Solution {
        public int Sum(int n){
            int sum = 0;
            while(n!=0){
                int t = n%10;
                sum+=t*t;
                n /= 10;
            }
            return sum;
        }
        public boolean isHappy(int n) {
            int slow = n;
            int quick = Sum(n);
            while(slow!=quick){
                slow = Sum(slow);
                quick = Sum(Sum(quick));
            }
            return slow==1;
        }
    }
}
